package com.infore.dataReceiving.util;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.infore.dataReceiving.dto.WaterInstructionsDataDto;
import com.infore.dataReceiving.enums.WaterQualityTargetLevel;


/**
 * 污染源标准计算工具
 * 
 * @author e
 *
 */
public class PolluteStandardUtil {

	public static final int invalidLevel = 999999;
	
	public static final int waterTempStandard = 12;

	/** 污染源标准等级 */
	private static final Map<String, Integer> LEVEL = new HashMap<String, Integer>() {
		private static final long serialVersionUID = 1L;

		{
			put("default", 0);
			put("one", 1);
			put("two", 2);
			put("three", 3);
		}
	};

	/** 因子等级标准 */
	private static final Map<String, List<Double>> projectPolluteStandard = new HashMap<String, List<Double>>() {
		private static final long serialVersionUID = 12L;

		{
			put("w01001", Arrays.asList(6.0, 0.0, 9.0));// PH
			put("w21003", Arrays.asList(5.0, 25.0, 0.0)); // 氨氮(水温>12)
//			put("w21003_low", Arrays.asList(5.0, 25.0, 0.0)); // 氨氮(水温<=12)
			put("w21011", Arrays.asList(0.5, 3.0, 5.0)); // 总磷
			put("w21001", Arrays.asList(15.0, 20.0, 0.0));// 总氮
			put("w01018", Arrays.asList(50.0, 100.0, 120.0)); // 化学需氧量
		}
	};

	public static void setPolluteStandard(List<WaterInstructionsDataDto> finalList) {
		String value = null;
		Integer level = null;
		Iterator<WaterInstructionsDataDto> it = finalList.iterator();
		while (it.hasNext()) {
			WaterInstructionsDataDto data = it.next();
			// 获取水温的数据值
			String waterTemp = data.getW01010();
			int maxLevel = 0;
			for (String code : projectPolluteStandard.keySet()) {
				try {
					Field field = data.getClass().getDeclaredField(code);
					// 对象的属性的访问权限设置为可访问
					field.setAccessible(true);
					value = field.get(data) == null ? null : field.get(data).toString();
					level = getPolluteLevel(code, value, waterTemp);
					if (level != null && level > maxLevel) {
						maxLevel = level;
					}
				} catch (NoSuchFieldException e) {
					continue;
				} catch (SecurityException e) {
					continue;
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
			if (maxLevel == 0)
				maxLevel = invalidLevel;
			data.setWaterQuality(maxLevel + "");
			int mb = 0;
            WaterQualityTargetLevel tar = WaterQualityTargetLevel.getDesc(data.getTargerWaterQuality());
            if(tar != null) {
                mb = Integer.valueOf(tar.getStatusCode());
            }
//            if(maxLevel<=mb) {
//            	data.setStationState("0");
//            }else {
//            	data.setStationState("1");
//            }
		}

	}

	private static Integer getPolluteLevel(String code, String value, String waterTemp) {
		if (code == null || value == null)
			return null;
		List<Double> levelList = null;
		if ("w21003".equals(code)) { // 氨氮
			if (waterTemp != null && !"".equals(waterTemp)) {
				if (Double.valueOf(waterTemp) > waterTempStandard) { // 水温数据大于12
					levelList = projectPolluteStandard.get(code);
				} else {
					levelList = Arrays.asList(8.0, 30.0, 0.0);
				}
			}
		} else {
			levelList = projectPolluteStandard.get(code);
		}
		double val = Double.valueOf(value);
		if (levelList != null && levelList.size() > 0) {
			for (int i = 0; i < levelList.size(); i++) {
				if (val <= levelList.get(i)) {
					return i + 1;
				}
			}
		}
		return LEVEL.get("default");

	}
}
